package com.trkj.service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.trkj.dao.DeptDAO;
import com.trkj.dao.EmpDAO;
import com.trkj.dao.OverworkDAO;
import com.trkj.dao.PositionDAO;
import com.trkj.pojo.Dept;
import com.trkj.pojo.Emp;
import com.trkj.pojo.Overwork;
import com.trkj.pojo.Position;
import com.trkj.vo.OverWorkVo;

/**
 * 加班申请
 * 
 * @author 彭帅
 *
 */
@Service
@Transactional
public class OverWorkService {
	@Autowired
	EmpDAO empDAO;
	@Autowired
	DeptDAO deptDAO;
	@Autowired
	PositionDAO positionDAO;
	@Autowired
	OverworkDAO overworkDAO;
	@Autowired
	WorkFlowService workFlowService;

	public OverWorkVo overWorkVo(Integer empId) {
		try {
			SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
			Emp emp = empDAO.findOne(empId);
			OverWorkVo overWorkVo = new OverWorkVo();
			overWorkVo.setEmpId(emp.getEmpId());
			overWorkVo.setEmpName(emp.getEmpName());
			overWorkVo.setDeptId(emp.getDept().getDeptId());
			overWorkVo.setDeptName(emp.getDept().getDeptName());
			overWorkVo.setPositionId(emp.getPosition().getPositionId());
			overWorkVo.setPositionName(emp.getPosition().getPositionName());
			overWorkVo.setLunchPeople(emp.getEmpId());
			Date date = simpleDateFormat.parse(simpleDateFormat.format(new Date()));
			int a = date.getYear() + 1900;
			int b = date.getMonth() + 1;
			int c = date.getDate();
			System.out.println(date);
			overWorkVo.setOverworkUptime(a + "-" + b + "-" + c);
			return overWorkVo;
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return null;

	}

	/**
	 * 加班申请保存
	 * 
	 * @param overwork
	 * @param positionId
	 * @param empId
	 * @param deptId
	 * @return
	 */
	public Integer save_overwork(Overwork overwork, Integer positionId, Integer empId, Integer deptId) {
		try {
			if (overwork != null) {
				if (positionId != null) {
					Position position = positionDAO.findOne(positionId);
					overwork.setPosition(position);
				}
				if (empId != null) {
					Emp emp = empDAO.findOne(empId);
					overwork.setEmp(emp);
				}
				if (deptId != null) {
					Dept dept = deptDAO.findOne(deptId);
					overwork.setDept(dept);
				}
				return overworkDAO.save(overwork).getOverworkId();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return 0;
	}

	/**
	 * 提交加班申请
	 * 
	 * @param overwork
	 * @param empId
	 * @param deptId
	 * @param positionId
	 * @param useid
	 * @param processDefinitionKey
	 * @return
	 */
	public boolean submit_overwork(Overwork overwork, Integer empId, Integer deptId, Integer positionId, Integer useid,
			String processDefinitionKey) {
		if (isOverWork(overwork, useid, processDefinitionKey)) {
			Integer processId = save_overwork(overwork, positionId, empId, deptId);
			workFlowService.startEmployProcess(processId, useid, processDefinitionKey);
			return true;
		}
		return false;
	}

	/**
	 * 判断能否继续申请加班
	 * 
	 * @param empId
	 * @param processDefinitionKey
	 * @return
	 */
	public boolean isOverWork(Overwork newoverwork, Integer empId, String processDefinitionKey) {
		Integer processId = workFlowService.getRecentlyProcess(empId, processDefinitionKey);
		if (processId != null) {
			Overwork overwork = overworkDAO.findOne(processId);
			if (overwork != null && newoverwork != null) {
				Date start = overwork.getOverworkStarttime();
				Date end = overwork.getOverworkEndtime();
				Date nowStart = newoverwork.getOverworkStarttime();
				Date nowEnd = newoverwork.getOverworkEndtime();
				if (start.getTime() > nowEnd.getTime() || end.getTime() < nowStart.getTime()) {
					return true;
				}
			}
		} else {
			return true;
		}
		return false;

	}

	/**
	 * 提交加班申请
	 * 
	 * @param overwork
	 * @param empId
	 * @param deptId
	 * @param positionId
	 * @param useid
	 * @param processDefinitionKey
	 * @return
	 */
	public boolean submit_overwork(Integer useid, Integer processId, String processDefinitionKey) {
		Overwork overwork = overworkDAO.findOne(processId);
		if (isOverWork(overwork, useid, processDefinitionKey)) {
			workFlowService.startEmployProcess(processId, useid, processDefinitionKey);
			return true;
		}
		return false;
	}
}
